package com.example.myapplication
import android.animation.Animator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.animation.AnimationUtils
import android.view.animation.DecelerateInterpolator
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ProgressBar
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.R
import com.example.myapplication.ItemBean
import com.example.myapplication.adapter

class weather : AppCompatActivity() {

    private var rootView: ConstraintLayout? = null

    /** RecycleView 实例 */
    private var recycleView: RecyclerView? = null

    /** RecycleView 的适配器 */
    private var adapter: adapter? = null

    /** 使用帧动画实现的loading */
    private var frameAnimLoading:ImageView? = null
    /** 使用补间动画实现的loading */
    private var rotateAnimLoading:ImageView? = null
    private var originalBackgroundDrawable: Drawable? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_weather)
        rootView = findViewById(R.id.root)
        originalBackgroundDrawable = rootView?.background

        // 添加RecycleView
       // showRotateLoading()
        addRecycleView()
    }

    /**
     * 添加RecycleView
     */
    private fun addRecycleView() {
        // 从布局中进行获取
        recycleView = findViewById(R.id.rv_demo)
        adapter = adapter(createDemoDate())
        recycleView?.adapter = adapter
        recycleView?.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
    }
    //    /**
//     * 展示使用帧动画实现的loading
//     */
//    private fun showFrameAnimLoading() {
//        frameAnimLoading = findViewById(R.id.iv_loading)
//        (frameAnimLoading?.background as? AnimationDrawable)?.start()
//    }
//
//    /**
//     * 展示补间动画实现的loading
//     */
    @SuppressLint("ResourceType")
//    private fun showRotateLoading() {
//        rotateAnimLoading = findViewById(R.id.iv_loading)
//        rootView?.setBackgroundColor(Color.BLACK)
//
//        val anim = AnimationUtils.loadAnimation(this, R.anim.rotate_anim)
//        anim.interpolator = LinearInterpolator()
//        rotateAnimLoading?.startAnimation(anim)
//
//        Handler().postDelayed({
//            rootView?.background = originalBackgroundDrawable// 恢复背景颜色为白色或你想要的颜色
//            rotateAnimLoading?.clearAnimation()
//            rotateAnimLoading?.visibility = View.GONE
//        }, 1000) // 延迟3秒后执行隐藏加载动画的任务
//
//    }
//
    /**
     * 展示属性动画
     */
    private fun showValueAnim() {
        val view = findViewById<ImageView>(R.id.imageView4)
        val valueAnim = ValueAnimator.ofInt(0, 100)
        valueAnim?.duration = 300L
        // 加速
//        valueAnim?.interpolator = AccelerateInterpolator(10f)
        // 减速
        valueAnim?.interpolator = DecelerateInterpolator(10f)
        valueAnim?.addUpdateListener {
            val currentValue = it.animatedValue as Int
            //view.alpha = currentValue / 100f
            view.translationY = currentValue * 10f
        }

        valueAnim?.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {

            }

            override fun onAnimationEnd(animation: Animator, isReverse: Boolean) {

            }


            override fun onAnimationEnd(animation: Animator) {

            }


            override fun onAnimationCancel(animation: Animator) {

            }


            override fun onAnimationRepeat(animation: Animator) {

            }

        })

        valueAnim?.start()

    }

    private fun createDemoDate(): ArrayList<ItemBean> {

        val list = ArrayList<ItemBean>()
        val Item = ItemBean(
            R.drawable.taiyang,
            "现在",
            "1级"
        )
        val Item1 = ItemBean(
            R.drawable.duoyun,
            "11时",
            "1级"
        )
        val Item2 =
            ItemBean(
                R.drawable.duoyun,
                "12时",
                "2级"
            )

        val Item3 = ItemBean(
            R.drawable.taiyang,
            "13时",
            "3级"
        )
        val Item4 = ItemBean(
            R.drawable.taiyang,
            "14时",
            "2级"
        )
        val Item5 = ItemBean(
            R.drawable.taiyang,
            "15时",
            "2级"
        )
        val Item6 = ItemBean(
            R.drawable.taiyang,
            "16时",
            "2级"
        )
        val Item7 = ItemBean(
            R.drawable.duoyun,
            "17时",
            "1级"
        )
        val Item8 = ItemBean(
            R.drawable.duoyun,
            "18时",
            "1级"
        )
        list.add(Item)
            list.add(Item1)
            list.add(Item2)
            list.add(Item3)
            list.add(Item4)
        list.add(Item5)
        list.add(Item6)
        list.add(Item7)
        list.add(Item8)

        return list
    }


    fun startAnim(view: View) {
        // 帧动画
//        showFrameAnimLoading()
        // 补间动画
       // showRotateLoading()
        // 属性动画
        //showValueAnim()
    }
    private val LOADING_DELAY: Long = 300 // 300ms
    private lateinit var progressBar: ProgressBar

    // 点击跳转事件处理方法
    fun onImageViewClick(view: View) {
        // 显示 loading 界面
        progressBar = findViewById(R.id.progressBar2)
        progressBar.visibility = View.VISIBLE
        val linearLayout1 =findViewById<LinearLayout>(R.id.ll5)
        linearLayout1?.setBackgroundColor(Color.WHITE)

        // 延迟 300ms 后跳转到目标 Activity
        Handler(Looper.getMainLooper()).postDelayed({
            // 隐藏 loading 界面
            progressBar.visibility = View.GONE
            //linearLayout1?.setBackgroundColor(Color.TRANSPARENT)

            // 跳转到目标 Activity
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }, LOADING_DELAY) // 延迟 300ms
    }

    fun showToast(view: View) {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
    }

}